package com.swz.dcrm.ui.main;


import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.View;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.swz.dcrm.R;
import com.swz.dcrm.adpter.UtilAdapter;
import com.swz.dcrm.model.BaseResponse;
import com.swz.dcrm.model.MonthSummary;
import com.swz.dcrm.model.Order;
import com.swz.dcrm.ui.LoginActivity;
import com.swz.dcrm.ui.MainActivity;
import com.swz.dcrm.ui.base.BaseFragment;
import com.swz.dcrm.ui.viewmodel.AnalyzeViewModel;
import com.swz.dcrm.ui.viewmodel.MainViewModel;
import com.swz.dcrm.util.CustomDecoration;
import com.swz.dcrm.util.Tool;
import com.xh.baselibrary.callback.OnClickListener;
import com.xh.baselibrary.widget.BounceScrollView;
import com.xh.baselibrary.widget.CircleImageView;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment {

    @BindView(R.id.scroll)
    BounceScrollView scrollView;
    @BindView(R.id.toolbar)
    ConstraintLayout toolbar;
    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.c_user)
    ConstraintLayout cUser;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_post)
    TextView tvPost;
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.tv_store_name)
    TextView tvStoreName;
    @BindView(R.id.iv_headPic)
    CircleImageView ivHeadPic;
    @BindView(R.id.tv_customer_num)
    TextView tvCustomerNum;
    @BindView(R.id.tv_order_num)
    TextView tvOrderNum;
    @BindView(R.id.tv_delivery_num)
    TextView tvDeliveryNum;
    @BindView(R.id.tv_defeated_num)
    TextView tvDefeatedNum;
    @BindView(R.id.tv_month_level1)
    TextView tvMonthLevel1;
    @BindView(R.id.tv_month_level2)
    TextView tvMonthLevel2;
    @BindView(R.id.tv_month_level3)
    TextView tvMonthLevel3;
    @BindView(R.id.tv_month_level4)
    TextView tvMonthLevel4;
    @BindView(R.id.tv_customer_name1)
    TextView tvCustomerName1;
    @BindView(R.id.tv_customer_name2)
    TextView tvCustomerName2;
    @BindView(R.id.tv_customer_name3)
    TextView tvCustomerName3;
    @BindView(R.id.tv_customer_name4)
    TextView tvCustomerName4;


    @Inject
    MainViewModel mainViewModel;
    @Inject
    AnalyzeViewModel analyzeViewModel;
    private UtilAdapter utilAdapter;

    public static MineFragment getInstance() {
        return new MineFragment();
    }

    @Override
    public void onLoadRetry() {
        mainViewModel.getPersonalInfo().observe(this, personalInfoBaseResponse -> {
            if (personalInfoBaseResponse.isSuccess()) {
                tvName.setText(personalInfoBaseResponse.getData().getName());
                tvPost.setText(personalInfoBaseResponse.getData().getRole().getRoleGroup());
                tvStoreName.setText(personalInfoBaseResponse.getData().getCarShop().getName());
                Glide.with(getActivity())
                        .load(personalInfoBaseResponse.getData().getHeadPortraitUrl())
                        .into(ivHeadPic);
                if(utilAdapter==null&&mainViewModel.isManager()){
                    List<String> list=new ArrayList<>();
                    list.add(getString(R.string.mine_manager_util_1));
                    list.add(getString(R.string.mine_manager_util_2));
                    list.add(getString(R.string.mine_manager_util_3));
                    list.add(getString(R.string.mine_manager_util_4));
                    list.add(getString(R.string.mine_manager_util_5));
                    list.add(getString(R.string.mine_manager_util_6));
                    list.add(getString(R.string.mine_manager_util_7));
                    list.add(getString(R.string.mine_manager_util_8));
                    list.add(getString(R.string.mine_manager_util_9));
                    list.add(getString(R.string.mine_manager_util_10));
                    utilAdapter=new UtilAdapter(getActivity(),list);
                    utilAdapter.setOnClickListener(listener);
                    rv.setAdapter(utilAdapter);
                }else if(utilAdapter==null&&!mainViewModel.isManager()){
                    List<String> list=new ArrayList<>();
                    list.add(getString(R.string.mine_manager_util_5));
                    list.add(getString(R.string.mine_manager_util_6));
                    list.add(getString(R.string.mine_manager_util_7));
                    utilAdapter=new UtilAdapter(getActivity(),list);
                    utilAdapter.setOnClickListener(listener);
                    rv.setAdapter(utilAdapter);}

            }
        });
        analyzeViewModel.getMonthSummary().observe(this, monthSummaryBaseResponse -> {
            if(monthSummaryBaseResponse!=null&&monthSummaryBaseResponse.isSuccess()){
                tvCustomerNum.setText(monthSummaryBaseResponse.getData().getPotentialCustomerCount()+"");
                tvDefeatedNum.setText(monthSummaryBaseResponse.getData().getDefeatedCount()+"");
                tvOrderNum.setText(monthSummaryBaseResponse.getData().getOrderCount()+"");
                tvDeliveryNum.setText(monthSummaryBaseResponse.getData().getDeliveryCarCount()+"");
                tvMonthLevel1.setText(monthSummaryBaseResponse.getData().getClueCount()+"");
                tvMonthLevel2.setText(monthSummaryBaseResponse.getData().getComeShopCount()+"");
                tvMonthLevel3.setText(monthSummaryBaseResponse.getData().getQuotePriceCount()+"");
                tvMonthLevel4.setText(monthSummaryBaseResponse.getData().getPlaceOrderCount()+"");
                tvCustomerName1.setText(monthSummaryBaseResponse.getData().getClevelCount()+"");
                tvCustomerName2.setText(monthSummaryBaseResponse.getData().getBlevelCount()+"");
                tvCustomerName3.setText(monthSummaryBaseResponse.getData().getAlevelCount()+"");
                tvCustomerName4.setText(monthSummaryBaseResponse.getData().getHlevelCount()+"");

            }
        });
    }
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        onLoadRetry();
    }


    @Override
    public int layoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    public boolean initView() {
        getDigger().inject(this);
        toolbar.setAlpha(0);
        rv.setLayoutManager(new GridLayoutManager(getActivity(),4));
        rv.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {

                if (parent.getChildPosition(view) >3){
                    outRect.top = Tool.dip2px(view.getContext(),10);
                }

            }
        });
        ConstraintLayout.LayoutParams layoutParams= (ConstraintLayout.LayoutParams) toolbar.getLayoutParams();
        layoutParams.height=getStatusBarHeight(getContext())+ Tool.dip2px(getActivity(),50);
        toolbar.setLayoutParams(layoutParams);
        toolbar.setPadding(0,getStatusBarHeight(getContext()),0,0);
        title.setText(getResources().getStringArray(R.array.main_tab_name)[2]);
        mainViewModel.getHeadPic().observe(this,s -> {
            if(s!=null){
                Glide.with(getActivity())
                        .load(s)
                        .into(ivHeadPic);
            }
        });
        scrollView.setOnScrollChangeListener((BounceScrollView.OnScrollChangeListener) (v, scrollX, scrollY, oldScrollX, oldScrollY) -> {

            if (scrollY <= cUser.getBottom() / 2) {

                float y = scrollY;
                float bottom = (float) cUser.getBottom() / 2;
                toolbar.setAlpha(y / bottom);
            }
        });
        return false;
    }
    @OnClick(R.id.c_user)
    public void user() {
        go(mainViewModel, R.id.action_mainFragment_to_infomationFragment, null);
    }

    @OnClick(R.id.tv_Idcard)
    public void idCard() {
        go(mainViewModel, R.id.action_mainFragment_to_myIdCardFragment, null);
    }

    OnClickListener<String> listener=new OnClickListener<String>() {
        @Override
        public void onItemClick(String s) {
            if(s.equals(getString(R.string.mine_manager_util_1))){
                go(mainViewModel, R.id.action_mainFragment_to_staffManagementFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_2))){
                go(mainViewModel, R.id.action_mainFragment_to_targetFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_3))){
                go(mainViewModel, R.id.action_mainFragment_to_groupManagementFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_4))){
                go(mainViewModel, R.id.action_mainFragment_to_announcementFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_5))){
                go(mainViewModel, R.id.action_mainFragment_to_scheduleFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_6))){
                go(mainViewModel, R.id.action_mainFragment_to_addressBookFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_7))){
                go(mainViewModel, R.id.action_mainFragment_to_approvalFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_8))){
                go(mainViewModel, R.id.action_mainFragment_to_businessAnalysisFragment, null);
            }else if(s.equals(getString(R.string.mine_manager_util_9))){
                go(mainViewModel,R.id.action_mainFragment_to_dailyFragment,null);
            }else if(s.equals(getString(R.string.mine_manager_util_10))){
                go(mainViewModel,R.id.action_mainFragment_to_monthlyFragment,null);
            }
        }
    };


    @OnClick(R.id.tv_logout)
    public void logout() {
        Intent intent = new Intent(getActivity(), LoginActivity.class);
        startActivity(intent);
        getActivity().finish();
    }

}
